package boerse.main.Gui;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import boerse.main.Asset.Share;
import java.util.Timer;
import java.util.TimerTask;

@SuppressWarnings("serial")
public class DrawGraph extends JPanel {
    private static final int MAX_SCORE = 100;
    private static final int PREF_W = 800;
    private static final int PREF_H = 650;
    private static final int BORDER_GAP = 30;
    private static final Color GRAPH_COLOR = Color.red;
    private static final Color GRAPH_POINT_COLOR = Color.black;
    private static final Stroke GRAPH_STROKE = new BasicStroke(3f);
    private static final int GRAPH_POINT_WIDTH = 12;
    private static final int Y_HATCH_CNT = 10;
    private List<Integer> scores;
    private Share share;
    private int base;
    private Timer timer;
    private static final int TICK_PERIOD = 1000;
    private static JFrame frame;
    private static DrawGraph mainPanel;
    private int counter = 0;

    public DrawGraph(Share share, Timer timer) {
        this.share = share;
        this.timer = timer;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        double xScale = ((double) getWidth() - 2 * BORDER_GAP) / (scores.size() - 1);
        double yScale = ((double) getHeight() - (2 * BORDER_GAP)) / (MAX_SCORE - 1);

        List<Point> graphPoints = new ArrayList<Point>();
        for (int i = 0; i < scores.size(); i++) {
            int x1 = (int) (i * xScale + BORDER_GAP);
            int y1 = (int) ((this.MAX_SCORE - BORDER_GAP/50) - ((scores.get(i)/10) * yScale - BORDER_GAP));;
            graphPoints.add(new Point(x1, y1));
        }

        // create x and y axes
        g2.drawLine(BORDER_GAP, getHeight() - BORDER_GAP, BORDER_GAP, BORDER_GAP);
        g2.drawLine(BORDER_GAP, getHeight() - BORDER_GAP, getWidth() - BORDER_GAP, getHeight() - BORDER_GAP);

        // create hatch marks for y axis.
        for (int i = 0; i < Y_HATCH_CNT; i++) {
            int x0 = BORDER_GAP;
            int x1 = GRAPH_POINT_WIDTH + BORDER_GAP;
            int y0 = getHeight() - (((i + 1) * (getHeight() - BORDER_GAP * 2)) / Y_HATCH_CNT + BORDER_GAP);
            int y1 = y0;
            g2.drawLine(x0, y0, x1, y1);
        }

        // and for x axis
        for (int i = 0; i < scores.size() - 1; i++) {
            int x0 = (i + 1) * (getWidth() - BORDER_GAP * 2) / (scores.size() - 1) + BORDER_GAP;
            int x1 = x0;
            int y0 = getHeight() - BORDER_GAP;
            int y1 = y0 - GRAPH_POINT_WIDTH;
            g2.drawLine(x0, y0, x1, y1);
        }

        Stroke oldStroke = g2.getStroke();
        g2.setColor(GRAPH_COLOR);
        g2.setStroke(GRAPH_STROKE);
        for (int i = 0; i < graphPoints.size() - 1; i++) {
            int x1 = graphPoints.get(i).x;
            int y1 = graphPoints.get(i).y;
            int x2 = graphPoints.get(i + 1).x;
            int y2 = graphPoints.get(i + 1).y;
            g2.drawLine(x1, y1, x2, y2);
        }

        g2.setStroke(oldStroke);
        g2.setColor(GRAPH_POINT_COLOR);
        for (int i = 0; i < graphPoints.size(); i++) {
            int x = graphPoints.get(i).x - GRAPH_POINT_WIDTH / 2;
            int y = graphPoints.get(i).y - GRAPH_POINT_WIDTH / 2;;
            int ovalW = GRAPH_POINT_WIDTH;
            int ovalH = GRAPH_POINT_WIDTH;
            g2.fillOval(x, y, ovalW, ovalH);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(PREF_W, PREF_H);
    }

    private void createAndShowGui() {
        this.base = 0;
        while(share.getPrice()>base+30) base+=50;
        this.scores = new ArrayList<Integer>();
        int maxDataPoints = 16;
        for (int i = 0; i < maxDataPoints ; i++) {
            scores.add((int)share.getPrice()-base);
        }
        frame = new JFrame("DrawGraph");
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public void runGraph() {
        createAndShowGui();
        timer.scheduleAtFixedRate(new TickerTask(), 1000, TICK_PERIOD);
    }

    public void start(DrawGraph drawGraph, Share share, Timer timer) {
        this.timer = timer;
        this.share = share;
        this.mainPanel = drawGraph;
        runGraph();
    }


    private class TickerTask extends TimerTask {
        public void run() {
            scores.remove(0);
            scores.add((int)share.getPrice()-base);
            checkBase((int)share.getPrice());
            frame.getContentPane().add(mainPanel);
            frame.repaint();
        }

        private void checkBase(int newV) {

                if (newV - base > 70) {
                    base += 50;
                    refactor(50);
                }
                else if (newV - base < 20) {
                     base -= 50;
                     refactor(-50);
                }


        }

        private void refactor(int value) {
            for (int i = 0; i < scores.size()-1; i++);
        }

    }
}